package com.fy.market.strategy.service.rule.filter.filterFactory;
import com.fy.market.strategy.model.entity.RuleActionEntity;
import com.fy.market.strategy.service.annotation.LogicStrategy;
import com.fy.market.strategy.service.rule.filter.ILogicFilter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fwq
 * @description 逻辑过滤工厂
 * @date 2025-09-04
 */
@Service
public class LogicFilterFactory {

    public Map<String, ILogicFilter> logicFilterMap = new ConcurrentHashMap<>();

    // Spring通过构造器注入所有ILogicFilter的实现类
    public LogicFilterFactory(List<ILogicFilter> logicFilters) {
        // logicFilters参数包含所有实现了ILogicFilter接口的Bean
        // 例如：[RuleWeightLogicFilter实例, RuleBackListLogicFilter实例]

        logicFilters.forEach(logic -> {
            // 通过反射获取类上的@LogicStrategy注解
            LogicStrategy strategy = AnnotationUtils.findAnnotation(logic.getClass(), LogicStrategy.class);

            if (null != strategy) {
                // 将注解中的规则类型作为key，Bean实例作为value存储
                logicFilterMap.put(strategy.ruleModel().getName(), logic);
                // 结果：{"rule_weight" -> RuleWeightLogicFilter实例, "rule_blacklist" -> RuleBackListLogicFilter实例}
            }
        });
    }

    /**
     * 逻辑过滤工厂调用开口
     * @return 逻辑过滤Map
     */
    public <T extends RuleActionEntity.RaffleEntity> Map<String, ILogicFilter> openLogicFilter() {
        return  logicFilterMap;
    }

}
